﻿using System.Collections.Generic;
using Eagles.Base.DataBase;
using Eagles.Application.Model.Enums;
using Eagles.DomainService.Model.News;
using Eagles.DomainService.Model.User;
using Eagles.Interface.DataAccess.StudyAccess;

namespace Ealges.DomianService.DataAccess.Study
{
    public class StudyDataAccess : IStudyAccess
    {
        private readonly IDbManager dbManager;

        public StudyDataAccess(IDbManager dbManager)
        {
            this.dbManager = dbManager;
        }

        public int EditStudyTime(bool exists, TbUserStudyLog userStudy)
        {
            if (exists)
            {
                return dbManager.Excuted(@"update eagles.tb_user_study_log set StudyTime = StudyTime + @StudyTime where UserId = @UserId ", userStudy);
            }
            else
            {
                return dbManager.Excuted(@"insert into eagles.tb_user_study_log (OrgId,BranchId,UserId,NewsId,ModuleId,StudyTime,CreateTime) values 
(@OrgId,@BranchId,@UserId,@NewsId,@ModuleId,@StudyTime,@CreateTime)", userStudy);
            }
        }

        public TbUserStudyLog GetStudyTime(int userId, int newsId, int moduleId)
        {
            return dbManager.QuerySingle<TbUserStudyLog>(@"select StudyTime from eagles.tb_user_study_log where UserId = @UserId and NewsId = @NewsId and ModuleId = @ModuleId ",
                new {UserId = userId, NewsId = newsId, ModuleId = moduleId});
        }

        public List<TbNews> GetUserStudy(StudyEnum studyEnum, int orgId, int userId, int pageIndex = 1, int pageSize = 10)
        {
            int pageIndexParameter = (pageIndex - 1) * pageSize;
            switch (studyEnum)
            {
                case StudyEnum.Learned:
                    return dbManager.Query<TbNews>(@"select a.NewsId,a.Module,a.ShortDesc,a.Title,a.HtmlContent,a.Author,a.ImageUrl,a.CreateTime,a.StudyTime,b.StudyTime from eagles.tb_news a 
left join eagles.tb_user_study_log b on a.NewsId = b.NewsId where a.OrgId = @OrgId and a.Status = 0 and a.Canstudy =1 and b.StudyTime >= a.StudyTime and b.UserId = @UserId order by a.CreateTime desc limit @PageIndex, @PageSize", 
new {OrgId = orgId, UserId = userId, PageIndex = pageIndexParameter, PageSize = pageSize});
                case StudyEnum.Unlearned:
                    return dbManager.Query<TbNews>(@"select a.NewsId,a.Module,a.ShortDesc,a.Title,a.HtmlContent,a.Author,a.ImageUrl,a.CreateTime ,ifnull(b.StudyTime,0) as StudyTime from eagles.tb_news a 
left join eagles.tb_user_study_log b on a.NewsId = b.NewsId and b.UserId = @UserId where a.OrgId = @OrgId and a.Status = 0 and a.StudyTime > ifnull(b.StudyTime,0)
and a.CanStudy =1 order by a.CreateTime desc limit @PageIndex, @PageSize", new { OrgId = orgId, UserId= userId, PageIndex = pageIndexParameter, PageSize = pageSize });
            }
            return null;
        }

        public int GetUserStudyCount(StudyEnum studyEnum, int orgId, int userId)
        {
            switch (studyEnum)
            {
                case StudyEnum.Learned:
                    return dbManager.ExecuteScalar<int>(@"select Count(*) StudyCount from eagles.tb_news a 
left join eagles.tb_user_study_log b on a.NewsId = b.NewsId where a.OrgId = @OrgId and a.Status = 0 and a.Canstudy =1 and b.StudyTime >= a.StudyTime and b.UserId = @UserId ", new { OrgId = orgId, UserId = userId });
                case StudyEnum.Unlearned:
                    return dbManager.ExecuteScalar<int>(@"select Count(*) from eagles.tb_news a 
left join eagles.tb_user_study_log b on a.NewsId = b.NewsId and b.UserId = @UserId where a.OrgId = @OrgId and a.Status = 0 and a.StudyTime > ifnull(b.StudyTime,0) and a.CanStudy =1 ", new { OrgId = orgId, UserId = userId });
            }
            return 0;
        }

        public TbNews GetUserStudyDetail(int newsId, int moduleId)
        {
            return dbManager.QuerySingle<TbNews>(@"select a.NewsId,a.Title,a.HtmlContent,a.Author,a.ImageUrl,a.CreateTime,
(select StudyTime from tb_user_study_log b where a.NewsId = b.NewsId) StudyTime from eagles.tb_news a where a.NewsId = @NewsId and a.Module = @Module ",
                new { NewsId = newsId, Module = moduleId });
        }
    }
}